package linkeklist;

public class DoubleLinkedListDemo {
	public static void main(String[] args) {
		System.out.println("双向链表的测试");

		//创建节点
		HeroNode hero1 = new HeroNode(1, "A", "a");
		HeroNode hero2 = new HeroNode(2, "B", "b");
		HeroNode hero3 = new HeroNode(3, "C", "c");
		HeroNode hero4 = new HeroNode(5, "D", "d");

		//创建一个双向链表对象
		DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
		doubleLinkedList.add(hero1);
		doubleLinkedList.add(hero2);
		doubleLinkedList.add(hero3);
		doubleLinkedList.add(hero4);

		doubleLinkedList.list();

//        //修改测试
//        linkeklist.HeroNode newHero = new linkeklist.HeroNode(2,"BBB","bbb");
//        doubleLinkedList.update(newHero);
//        //修改后的
//        doubleLinkedList.list();

//        //删除
//        doubleLinkedList.delete(3);
//        doubleLinkedList.list();

		//按顺序添加
		HeroNode newHero2 = new HeroNode(4, "!!!!", "!!!!");
		doubleLinkedList.addByNo(newHero2);
		doubleLinkedList.list();
		System.out.println(hero3.next);
	}
}

//创建一个双向链表类
class DoubleLinkedList {
	//初始化
	private HeroNode head = new HeroNode(0, "", "");

	//返回头节点
	public HeroNode getHead() {
		return head;
	}

	//遍历双向链表
	public void list() {
		//判断是否为空
		if (head.next == null) {
			System.out.println("链表为空");
			return;
		}
		HeroNode temp = head.next;
		while (true) {
			if (temp == null) {
				break;
			}
			System.out.println(temp);
			temp = temp.next;
		}
	}

	//添加
	public void add(HeroNode heroNode) {
		HeroNode temp = head;
		while (true) {//找到链表最后
			//判断是否还有下一个
			if (temp.next == null) {
				break;
			}
			//若还有下一个，指针后移
			temp = temp.next;
		}
		//找到最后一个节点，退出循环
		temp.next = heroNode;
		heroNode.pre = temp;
	}

	//按序号添加
	public void addByNo(HeroNode heroNode) {
		HeroNode temp = head;
		while (true) {
			if (temp.no == heroNode.no) {
				System.out.println("编号已存在");
				return;
			}
			if (temp.next == null) {
				temp.next = heroNode;
				heroNode.pre = temp;
				break;
			}
			if (heroNode.no > temp.no && heroNode.no < temp.next.no) {
				heroNode.pre = temp;
				heroNode.next = temp.next;
				temp.next.pre = heroNode;
				temp.next = heroNode;
				break;
			}
			temp = temp.next;
		}
	}

	//修改节点
	public void update(HeroNode newheroNode) {
		//判断是否为空
		if (head.next == null) {
			System.out.println("链表为空");
			return;
		}
		//找到需要修改的节点
		HeroNode temp = head.next;
		boolean flag = false;//表示是否找到该节点
		while (true) {
			if (temp == null) {
				break;
			}
			if (temp.no == newheroNode.no) {//满足就找到了
				flag = true;
				break;
			}
			temp = temp.next;
		}
		//根据flag判断是否找到需要修改的节点
		if (flag) {
			temp.name = newheroNode.name;
			temp.nickName = newheroNode.nickName;
		} else {//没有找到
			System.out.printf("没有找到 编号 %d 的节点。", newheroNode.no);
		}
	}

	//删除节点
	public void delete(int no) {
		if (head.next == null) {
			System.out.println("链表为空");
			return;
		}

		HeroNode temp = head.next;
		boolean flag = false;
		while (true) {
			if (temp == null) {
				break;
			}
			if (temp.no == no) {//满足就找到了
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if (flag) {
			//已找到需要删除的节点，并删除
			temp.pre.next = temp.next;
			if (temp.next != null) {//需要先判断是否时最后一个节点
				temp.next.pre = temp.pre;
			}
		} else {
			System.out.printf("没有找到 编号 %d 的节点。", no);
		}
	}
}

class HeroNode {
	public int no;
	public String name;
	public String nickName;
	public HeroNode next;//指向下一个节点
	public HeroNode pre;//指向前一个


	public HeroNode(int no, String name, String nickName) {
		this.no = no;
		this.name = name;
		this.nickName = nickName;
	}

	@Override
	public String toString() {
		return "linkeklist.HeroNode{" +
				"no=" + no +
				", name='" + name + '\'' +
				", nickName='" + nickName + '\'' +
				'}';
	}
}

